﻿using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Prism.Commands;
using Prism.Mvvm;

namespace SoonCode.FocusTest.ViewModels
{
    internal class InputBoxViewModel : BindableBase, IDialogAware
    {
        private string errMessage;

        public string ErrorMessage
        {
            get { return errMessage; }
            set
            {
                errMessage=value;
                RaisePropertyChanged();
            }
        }

        private string inputString = "";
        public string InputString
        {
            get { return inputString;}
            set
            {
                inputString = value;
                RaisePropertyChanged();
            }

        }
        
        private Func<string,bool> CheckAction;
        public InputBoxViewModel()
        {
            OkCommand = new DelegateCommand(OnOkCommand);
        }
        public string DialogTitle { get; set; } = "请输入";
        public string Message { get; set; }
        public string Title => DialogTitle;
        public event Action<IDialogResult> RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            
        }

        public ICommand OkCommand { get; set; }
        private void OnOkCommand()
        {
            if (CheckAction != null)
            {
                if (!CheckAction.Invoke(InputString))
                {
                    ErrorMessage = "输入错误";
                    return;
                }
            }
            DialogParameters parameters = new DialogParameters();
            parameters.Add("result",InputString);
            RequestClose.Invoke(new DialogResult(ButtonResult.Yes,parameters));
        }
        public void OnDialogOpened(IDialogParameters parameters)
        {
            DialogTitle = parameters.GetValue<string>("DialogTitle");
            Message= parameters.GetValue<string>("Message");
            InputString = parameters.GetValue<string>("DefaultValue");
            CheckAction = parameters.GetValue<Func<string,bool>>("CheckAction");
        }
    }
}
